<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组分割示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fc;
            margin: 0;
            padding: 0;
        }

        .container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            flex-direction: column;
        }

        h1 {
            color: #3b3f58;
            margin-bottom: 20px;
        }

        .btn {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
            border-radius: 5px;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #45a049;
        }

        .output {
            margin-top: 30px;
            width: 100%;
            display: flex;
            justify-content: space-around;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        .group {
            width: 45%;
            padding: 20px;
            background-color: #f0f4f8;
            border-radius: 8px;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
        }

        .group h2 {
            text-align: center;
            color: #3b3f58;
        }

        .group ul {
            list-style-type: none;
            padding: 0;
        }

        .group li {
            background-color: #e8f0fe;
            margin: 8px 0;
            padding: 10px;
            border-radius: 5px;
            font-size: 16px;
            color: #3b3f58;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>数组分割示例：根据数字是否为偶数分割数组</h1>
        <button class="btn" onclick="splitArray()">分割数组</button>

        <div class="output" id="output">
            <div class="group" id="evenGroup">
                <h2>偶数</h2>
                <ul id="evenList"></ul>
            </div>
            <div class="group" id="oddGroup">
                <h2>奇数</h2>
                <ul id="oddList"></ul>
            </div>
        </div>
    </div>

    <script>
        // bifurcateBy函数：将数组根据条件分割成两个数组
        const bifurcateBy = (arr, handler) =>
            arr.reduce(
                (acc, val, i) => (acc[handler(val, i) ? 0 : 1].push(val), acc),
                [[], []]
            );

        // 用于分割数组的条件：判断数字是否为偶数
        function splitArray() {
            // 示例数组
            const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

            // 调用bifurcateBy进行分割，判断条件是数字是否为偶数
            const [even, odd] = bifurcateBy(numbers, (val) => val % 2 === 0);

            // 更新页面展示结果
            displayResults(even, odd);
        }

        // 显示分割结果
        function displayResults(even, odd) {
            // 清空原有内容
            document.getElementById("evenList").innerHTML = '';
            document.getElementById("oddList").innerHTML = '';

            // 显示偶数
            even.forEach(num => {
                const li = document.createElement('li');
                li.textContent = num;
                document.getElementById("evenList").appendChild(li);
            });

            // 显示奇数
            odd.forEach(num => {
                const li = document.createElement('li');
                li.textContent = num;
                document.getElementById("oddList").appendChild(li);
            });
        }
    </script>
</body>

</html>